<template>
  <div class="app-container">
    <el-tabs v-model="activeName" @tab-click="getDataList">
      <el-tab-pane label="对分包罚款" name="first">
        
      </el-tab-pane>
      <el-tab-pane label="对项目扣分" name="second">
        
      </el-tab-pane>
    </el-tabs>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="创建时间">
        <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="创建人" prop="createBy">
        <el-select
              style="width: 160px"
              v-model="queryParams.createBy"
              @change="handleQuery"
            >
              <el-option
                v-for="(item,index) in revisePeopleList"
                :key="index"
                :value="item.userName"
                :label="item.nickName"
              ></el-option>
            </el-select>
      </el-form-item>
      <el-form-item v-if="activeName == 'first'" label="罚款对象" prop="fineUser">
        <el-select v-model="queryParams.fineUser" :placeholder="activeName == 'first' ? '请选择罚款对象' : '请选择扣分对象'" clearable>
          <el-option v-for="(item, index) in (activeName == 'first' ? listSelect : listSelectData)" :key="index"
            :label="activeName == 'first' ? item.enterprise_name : item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item :label="activeName == 'first' ? '罚款来源' : '扣分来源'" prop="fineSource">
        <el-select v-model="queryParams.fineSource" :placeholder="activeName == 'first' ? '请选择罚款来源' : '请选择扣分来源'" clearable>
          <el-option v-for="dict in dict.type.biz_fine_source" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['fine:fine:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['fine:fine:remove']">删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['fine:fine:export']">导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="fineList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="safeNumber" />
      <!-- <el-table-column :label="activeName == 'first' ? '罚款单位' : '扣分单位'" align="center" prop="fineUnit" /> -->
      <el-table-column :label="activeName == 'first' ? '罚款对象' : '扣分对象'" align="center" prop="fineUser" />
      <el-table-column :label="activeName == 'first' ? '罚款金额(元)' : '扣分分数(分)'" align="center" prop="fineMoney" />
      <el-table-column :label="activeName == 'first' ? '罚款说明' : '扣分说明'" align="center" prop="fineExplain" />

      <el-table-column :label="activeName == 'first' ? '罚款来源' : '扣分来源'" align="center" prop="fineSource">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.biz_fine_source" :value="scope.row.fineSource" />
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createByNickName" />
      <el-table-column label="创建日期" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
  
      <!--      <el-table-column label="附件" align="center" prop="fineAttachment" />-->
      <!--      <el-table-column label="罚款按钮" align="center" prop="fineButton" />-->
      <!--      <el-table-column label="项目id" align="center" prop="projectId" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['fine:fine:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['fine:fine:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getDataList" />

    <!-- 添加或修改质量罚款台账对话框 -->
    <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="open" v-if="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px" style="margin-right: 80px" :inline="true">
        <el-form-item label="编号:" class="inputDeep">
              <el-input v-model="form.safeNumber" readonly />
        </el-form-item>
        <div v-if="this.title == '修改质量罚款台账' || this.title == '修改质量扣分台账'">
          <el-form-item :label="activeName == 'first' ? '罚款单位:' : '扣分单位:'" class="inputDeep">
            <el-input v-model="form.fineUnit" readonly style="width: 380px" />
          </el-form-item>
          <el-row>
            <el-col :span="12">
              <el-form-item label="创建人:" class="inputDeep">
                <el-input v-model="form.createBy" readonly />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="创建日期:" class="inputDeep">
                <el-input v-model="form.createTime" readonly />
              </el-form-item>
            </el-col>
          </el-row>                  
          <el-row>
            <el-col :span="12">
              <el-form-item :label="activeName == 'first' ? '罚款来源:' : '扣分来源:'" class="inputDeep">
                <el-input v-model="form.fineSource" readonly />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item :label="activeName == 'first' ? '罚款对象:' : '扣分对象:'" class="inputDeep">
                <el-input v-model="form.fineUser" readonly />
              </el-form-item>
            </el-col>
          </el-row>
        </div>
        <div v-else>
          <el-form-item :label="activeName == 'first' ? '罚款来源:' : '扣分来源:'" prop="fineSource">
            <el-select style="width: 380px" v-model="form.fineSource" :placeholder="activeName == 'first' ? '请选择罚款来源' : '请选择扣分来源'" clearable>
              <el-option v-for="dict in dict.type.biz_fine_source" :key="dict.value" :label="dict.label"
                :value="dict.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="罚款对象:" v-if="activeName == 'first'" prop="fineUnit">
            <el-select style="width: 380px" v-model="form.fineUser" placeholder="请选择罚款单位" clearable>
              <el-option v-for="(item, index) in listSelect" :key="index" :label="item.enterprise_name"
                :value="item.id" />
            </el-select>
          </el-form-item>
          <!-- <el-form-item label="扣分对象:" v-else prop="fineUnit" >
            <el-select style="width: 380px" v-model="form.fineUser" placeholder="请选择扣分项目" clearable>
              <el-option v-for="(item, index) in listSelectData" :key="index" :label="item.name" :value="item.id" />
            </el-select>
          </el-form-item> -->
        </div>
        <el-form-item :label="activeName == 'first' ? '罚款金额(元):' : '扣分分数(分):'" prop="fineMoney">
          <el-input style="width: 380px" v-model="form.fineMoney" :placeholder="activeName == 'first' ? '请输入罚款金额(元)' : '请输入扣分分数(分)'" />
        </el-form-item>
        <el-form-item :label="activeName == 'first' ? '罚款说明:' : '扣分说明:'" prop="fineExplain">
          <el-input style="width: 380px" v-model="form.fineExplain" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="现场照片:" prop="fineAttachment">
          <!-- <file-upload v-model="form.fineAttachment" @addNum="addNum" @delNum="delNum" /> -->
          <ImageUpload v-model="form.fineAttachments" @addNumData="addNumData" @delNumData="delNumData"></ImageUpload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listFine,
  getFine,
  delFine,
  addProgram,
  updateFine,
  addSubPackage,
  subPackageList,
  programList,
  getlist,
  getlistData,
} from "@/api/biz/fine/fine";
import {
  getRevisePeople,
} from "@/api/biz/ledger/ledger";
import { mapState } from "vuex";
export default {
  name: "Fine",
  dicts: ["biz_fine_source"],
  data() {
    return {
      // 遮罩层
      loading: true,
      uploadList:[],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 质量罚款台账表格数据
      fineList: [],
      //下拉数据
      revisePeopleList:null,
      listSelect: null,
      listSelectData: null,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        safeNumber: null,
        fineUnit: null,
        fineUser: null,
        fineExplain: null,
        fineSource: null,
        fineMoney: null,
        fineAttachment: null,
        fineButton: null,
        projectId: this.$store.state.app.selectData,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        fineMoney: [
          { required: true, message: "不能为空", trigger: "blur" },
        ],
        fineSource: [
          { required: true, message: "不能为空", trigger: "change" },
        ],
      },
      activeName: "first",
      //生成编号
      code: null,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询质量罚款台账列表 */
    getList() {
      this.loading = true;
      subPackageList(this.addDateRange(this.queryParams, this.dateRange)).then(
        (response) => {
          this.fineList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
      getlist().then((data) => {
        this.listSelect = data;
      });
      getRevisePeople(this.queryParams).then((data) => {
          this.revisePeopleList = data.rows;
        });
      getlistData().then((data) => {
        this.listSelectData = data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        safeNumber: null,
        fineUnit: null,
        fineUser: null,
        fineExplain: null,
        fineSource: null,
        fineMoney: null,
        fineAttachment: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        fineButton: null,
        projectId: this.$store.state.app.selectData,
      };
      // this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      if (this.activeName == "first") {
        this.getList();
      } else {
        programList(this.addDateRange(this.queryParams, this.dateRange)).then(
          (response) => {
            this.fineList = response.rows;
            this.total = response.total;
            this.loading = false;
          }
        );
      }
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      if (this.activeName == "first") {
        this.title = "添加质量罚款台账";
        const data = {
          id: 1,
        };
        listFine(data).then((data) => {
          this.form.safeNumber = data.safeNumber;
          this.open = true;
        });
      } else {
        this.title = "添加质量扣分台账";
        const data = {
          id: 2,
        };
        listFine(data).then((data) => {
          this.form.safeNumber = data.safeNumber;
          this.open = true;
        });
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();

      if (this.activeName == "first") {

        this.title = "修改质量罚款台账";
      } else {

        this.title = "修改质量扣分台账";
      }
      getFine(row.id).then((response) => {
        this.form = response.data;
        if(this.form.fineAttachments!=null){
          this.form.fineAttachments.forEach((item) => {
            item.url = process.env.VUE_APP_MINIO_BASE_API+ '/'+ item.bucket + "/" + item.name
          })
          this.uploadList = this.form.fineAttachments.map(item=>item.id)
        }
        this.open = true;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            this.form.fineAttachment = this.uploadList.toString()
            this.form.fineAttachments = null
            updateFine(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getDataList();
            });
          } else {
            this.form.projectId=this.$store.state.app.selectData
            this.form.fineAttachments = null
            if (this.activeName == "first") {
              this.form.fineButton = "1";
              this.form.fineAttachment = this.uploadList.toString()
              addSubPackage(this.form).then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getDataList();
              });
            } else {
              this.form.fineAttachment = this.uploadList.toString()
              this.form.fineAttachments = null
              this.form.fineUser = this.$store.state.app.selectData
              this.form.fineButton = "2";
              addProgram(this.form).then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getDataList();
              });
            }
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;

      if (this.activeName == "first") {
        this.$modal
          .confirm('是否确认删除质量罚款台账编号为"' + ids + '"的数据项？')
          .then(function () {
            return delFine(ids);
          })
          .then(() => {
            this.getDataList();
            this.$modal.msgSuccess("删除成功");
          })
          .catch(() => { });
      } else {
        this.$modal
          .confirm('是否确认删除质量扣分台账编号为"' + ids + '"的数据项？')
          .then(function () {
            return delFine(ids);
          })
          .then(() => {
            this.getDataList();
            this.$modal.msgSuccess("删除成功");
          })
          .catch(() => { });
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      
      if (this.activeName == "first") {
      this.queryParams.fineButton = "1"
      } else {
      this.queryParams.fineButton = "2"
      }
      this.download(
        "fine/fine/export",
        {
          ...this.queryParams,
        },
        `fine_${new Date().getTime()}.xlsx`
      );
    },
    //点击查询
    getDataList() {
      this.loading = true;
      this.dateRange = [];
      this.resetForm("queryForm")
      if (this.activeName == "first") {
        subPackageList(
          this.addDateRange(this.queryParams, this.dateRange)
        ).then((response) => {
          this.fineList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      } else {
        programList(this.addDateRange(this.queryParams, this.dateRange)).then(
          (response) => {
            this.fineList = response.rows;
            this.total = response.total;
            this.loading = false;
          }
        );
      }
      getRevisePeople(this.queryParams).then((data) => {
          this.revisePeopleList = data.rows;
        });
    },
    addNumData(data){
      this.uploadList.push(data.fileId);
    },
    delNumData(data){
      this.uploadList.splice(data, 1)
    }
  },
  watch: {
    app: {
      handler(newVal, oldVal) {
        this.queryParams.projectId = newVal.selectData;
        this.getDataList();
      },
      deep: true,
    },
  },
  computed: {
    ...mapState(["app"]),
  }
};
</script>
<style scoped>
/* 利用穿透，设置input边框隐藏 */
.inputDeep>>>.el-input__inner {
  border: 0;
}
</style>
